package com.it;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteShellCommand {

    public static void main(String[] args) {
        try {
            // 使用ProcessBuilder执行命令
            ProcessBuilder builder = new ProcessBuilder("ls", "-l");
            Process process = builder.start();

            // 读取命令的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 打印输出结果
            }

            // 等待命令执行完成，并获取退出状态码
            int exitCode = process.waitFor();
            System.out.println("Command exited with code: " + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}